/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.struts;

import com.cashmate.hibernate.dao.*;
import com.cashmate.hibernate.pojo.*;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Dine
 */
public class GetProfileLoanApp extends ActionSupport {

    private static Logger logger = Logger.getLogger(GetAreas.class);
    private LoanapplicationDAO la = (LoanapplicationDAO) ServletActionContext.getServletContext().getAttribute("loanapplicationDAO");
    private MonthlyincomeDAO mid = (MonthlyincomeDAO) ServletActionContext.getServletContext().getAttribute("monthlyincomeDAO");
    private CreditinvestigationDAO cid = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    private AppraisalDAO apd = (AppraisalDAO) ServletActionContext.getServletContext().getAttribute("appraisalDAO");
    private LoansDAO ld = (LoansDAO) ServletActionContext.getServletContext().getAttribute("loansDAO");
    private LoanverificationDAO lvd = (LoanverificationDAO) ServletActionContext.getServletContext().getAttribute("loanverificationDAO");
    private BorrowerinfoDAO bid = (BorrowerinfoDAO) ServletActionContext.getServletContext().getAttribute("borrowerinfoDAO");
    private MonthlyincomeDAO md = (MonthlyincomeDAO) ServletActionContext.getServletContext().getAttribute("monthlyincomeDAO");
    private CreditinvestigationDAO cd = (CreditinvestigationDAO) ServletActionContext.getServletContext().getAttribute("creditinvestigationDAO");
    private RequirementDAO rd = (RequirementDAO) ServletActionContext.getServletContext().getAttribute("requirementDAO");
    private String actionstat;
    private String loanverid;
    //for app form
    private Borrowers borrowerinfo;
    private String borrowerid;
    private Loanapplications loaninfo;
    private Borrowerinfo borinfo;
    private List<Businesses> business;
    private List<Dependents> dependents;
    private List<Characterreferences> references;
    private List<Requirements> checkedreqs;
    private List<Requirements> uncheckedreqs;
    private Loanverification loanver;
    private Monthlyincome monthlyincome;
    //for ci
    private List<Vehicle> vehicles;
    private List<Othersourceofincome> other;
    private List<Personalandrealproperties> parp;
    private List<Sourceofincome> soi;
    private List<Informants> inf;
    private List<Documentspassed> doc;
    private List<Notesreceivable> notes;
    private List<Reputation> reputations;
    private List<Typeofcollateral> cols;
    private List<Loanswithotherbanks> lwob;
    private Creditinvestigations creditinv;
    //for ap
    private Appraisal appraisal;
    private List<Collaterals> colllist;

    public void setLoanverid(String loanverid) {
        this.loanverid = loanverid;
    }

    public String getActionstat() {
        return actionstat;
    }

    public Borrowerinfo getBorinfo() {
        return borinfo;
    }

    public String getBorrowerid() {
        return borrowerid;
    }

    public Borrowers getBorrowerinfo() {
        return borrowerinfo;
    }

    public List<Businesses> getBusiness() {
        return business;
    }

    public List<Dependents> getDependents() {
        return dependents;
    }

    public Loanapplications getLoaninfo() {
        return loaninfo;
    }

    public Loanverification getLoanver() {
        return loanver;
    }

    public Monthlyincome getMonthlyincome() {
        return monthlyincome;
    }

    public Creditinvestigations getCreditinv() {
        return creditinv;
    }

    public List<Documentspassed> getDoc() {
        return doc;
    }

    public List<Informants> getInf() {
        return inf;
    }

    public List<Othersourceofincome> getOther() {
        return other;
    }

    public List<Personalandrealproperties> getParp() {
        return parp;
    }

    public List<Sourceofincome> getSoi() {
        return soi;
    }

    public List<Vehicle> getVehicles() {
        return vehicles;
    }

    public List<Notesreceivable> getNotes() {
        return notes;
    }

    public Appraisal getAppraisal() {
        return appraisal;
    }

    public List<Typeofcollateral> getCols() {
        return cols;
    }

    public List<Reputation> getReputations() {
        return reputations;
    }

    public List<Collaterals> getColllist() {
        return colllist;
    }

    public List<Loanswithotherbanks> getLwob() {
        return lwob;
    }

    public List<Characterreferences> getReferences() {
        return references;
    }

    public List<Requirements> getCheckedreqs() {
        return checkedreqs;
    }

    public List<Requirements> getUncheckedreqs() {
        return uncheckedreqs;
    }

    @Action(value = "/get-profile-loanapp", results = {
        @Result(name = "success", type = "redirect", location = "../../loanprofile-loanapp.jsp")
    })
    public String getAreasList() {
        int lvid = Integer.parseInt(loanverid);
        try {
            loanver = lvd.get(lvid);
//            System.out.println(loanver.getLoanverificationid());
        } catch (Exception e) {
            actionstat = "Loan verification specified does not exist.";
            return ERROR;
        }

        //for application form
        if (loanver != null) {
            borrowerinfo = loanver.getBorrowers();
            borrowerid = borrowerinfo.getBorrowerid().toString();
//            System.out.println("BORROWERINFO: "+borrowerinfo);
            try {
                borinfo = bid.getLatestBorrowerinfo(borrowerinfo.getBorrowerid());
//                System.out.println("BORINFO: "+borinfo);
                loaninfo = la.getByLoanVerId(loanver.getLoanverificationid());
//                System.out.println("STATUSLOANVER:"+loaninfo.getStatus());
            } catch (Exception e) {
            }

            if (loaninfo != null) {
                try {
                    monthlyincome = md.getLatestMi(loaninfo.getLoanappid());
                } catch (Exception e) {
                }

                business = new ArrayList<Businesses>();
                Iterator i = loaninfo.getBusinesseses().iterator();
                while (i.hasNext()) {
                    Businesses bu = (Businesses) i.next();
                    business.add(bu);
//                    System.out.println(bu.getBusinessname());
                }

                dependents = new ArrayList<Dependents>();
                Iterator d = loaninfo.getDependentses().iterator();
                while (d.hasNext()) {
                    Dependents dep = (Dependents) d.next();
                    dependents.add(dep);
//                    System.out.println(dep.getName());
                }

                lwob = new ArrayList<Loanswithotherbanks>();
                Iterator l = loaninfo.getLoanswithotherbankses().iterator();
                while (l.hasNext()) {
                    Loanswithotherbanks lother = (Loanswithotherbanks) l.next();
                    lwob.add(lother);
//                    System.out.println(lother.getBankname());
                }

                references = new ArrayList<Characterreferences>();
                Iterator r = loaninfo.getCharacterreferenceses().iterator();
                while (r.hasNext()) {
                    Characterreferences cr = (Characterreferences) r.next();
                    references.add(cr);
//                    System.out.println(cr.getName());
                }

                checkedreqs = new ArrayList<Requirements>();
                Iterator chr = loaninfo.getLoanappRequirements().iterator();
                while (chr.hasNext()) {
                    LoanappRequirement lrq = (LoanappRequirement) chr.next();
                    Requirements rq = lrq.getRequirements();
                    checkedreqs.add(rq);
//                    System.out.println(rq.getRequirementname());
                }

                uncheckedreqs = new ArrayList<Requirements>();
                List<Requirements> allReqs = new ArrayList<Requirements>();
                try {
                    allReqs = rd.getActiveRequirements();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                boolean isMatch = false;
                boolean goOut = false;
                for(int k = 0; k < allReqs.size(); k++){
                    for(int j = 0; j < checkedreqs.size(); j++){
                        
                        if(allReqs.get(k).getRequirementid() == checkedreqs.get(j).getRequirementid()){
                            isMatch = true;
                        }else{
                            isMatch = false;
//                            System.out.println("k: " +allReqs.get(k).getRequirementid()+ " j: " + checkedreqs.get(j).getRequirementid());
                            uncheckedreqs.add(allReqs.get(k));
                        }
                        
                        if(isMatch = false){
                            break;
                        }else{
                            break;
                        }
                    }
                    
                }
                
//                System.out.println("UNCHECKED");
                for(int s = 0 ; s < uncheckedreqs.size(); s++){
//                    System.out.println(i + " : " + uncheckedreqs.get(s).getRequirementname());
                }
                
//                System.out.println("ALL REQS SIZE:" + allReqs.size());
//                System.out.println("CHECKED REQS SIZE:" + checkedreqs.size());
//                System.out.println("UNCHECKED REQS SIZE:" + uncheckedreqs.size());

            } else {
                actionstat = "noaf";
            }
        }

        //for credit investigation
        if (loanver != null) {
            try {
                creditinv = cd.getLatestByLoanverId(loanver.getLoanverificationid());
            } catch (Exception e) {
            }

            if (creditinv != null) {
                vehicles = new ArrayList<Vehicle>();
                other = new ArrayList<Othersourceofincome>();
                parp = new ArrayList<Personalandrealproperties>();
                notes = new ArrayList<Notesreceivable>();
                inf = new ArrayList<Informants>();
                doc = new ArrayList<Documentspassed>();
                reputations = new ArrayList<Reputation>();
                soi = new ArrayList<Sourceofincome>();
                cols = new ArrayList<Typeofcollateral>();

                if (creditinv.getVehicles() != null) {
                    Iterator v = creditinv.getVehicles().iterator();
                    while (v.hasNext()) {
                        Vehicle ve = (Vehicle) v.next();
                        vehicles.add(ve);
//                        System.out.println(ve.getVehicleid());
                    }
                }

                if (creditinv.getOthersourceofincomes() != null) {
                    Iterator o = creditinv.getOthersourceofincomes().iterator();
                    while (o.hasNext()) {
                        Othersourceofincome other1 = (Othersourceofincome) o.next();
                        other.add(other1);
//                        System.out.println(other1.getType());
                    }
                }

                if (creditinv.getPersonalandrealpropertieses() != null) {
                    Iterator p = creditinv.getPersonalandrealpropertieses().iterator();
                    while (p.hasNext()) {
                        Personalandrealproperties per = (Personalandrealproperties) p.next();
                        parp.add(per);
                    }
                }

                if (creditinv.getNotesreceivables() != null) {
                    Iterator n = creditinv.getNotesreceivables().iterator();
                    while (n.hasNext()) {
                        Notesreceivable no = (Notesreceivable) n.next();
                        notes.add(no);
                    }
                }

                if (creditinv.getInformantses() != null) {
                    Iterator in = creditinv.getInformantses().iterator();
                    while (in.hasNext()) {
                        Informants infor = (Informants) in.next();
                        inf.add(infor);
                    }
                }

                if (creditinv.getDocumentspasseds() != null) {
                    Iterator dp = creditinv.getDocumentspasseds().iterator();
                    while (dp.hasNext()) {
                        Documentspassed dpassed = (Documentspassed) dp.next();
                        doc.add(dpassed);
                    }
                }

                if (creditinv.getReputationCreditinvs() != null) {
                    Iterator rc = creditinv.getReputationCreditinvs().iterator();
                    while (rc.hasNext()) {
                        ReputationCreditinv repcre = (ReputationCreditinv) rc.next();
                        Reputation r = repcre.getReputation();
                        reputations.add(r);
                    }
                }

                if (creditinv.getSourceCreditinvs() != null) {
                    Iterator isoi = creditinv.getSourceCreditinvs().iterator();
                    while (isoi.hasNext()) {
                        SourceCreditinv sci = (SourceCreditinv) isoi.next();
                        Sourceofincome s = sci.getSourceofincome();
                        soi.add(s);
                    }
                }

                if (creditinv.getCollateralCreditinvs() != null) {
                    Iterator col = creditinv.getCollateralCreditinvs().iterator();
                    while (col.hasNext()) {
                        CollateralCreditinv cci = (CollateralCreditinv) col.next();
                        Typeofcollateral cc = cci.getTypeofcollateral();
                        cols.add(cc);
                    }
                }
            } else {
                actionstat = "noci";
            }

            if (loanver != null) {
                try {
                    appraisal = apd.getLatestByLoanverId(loanver.getLoanverificationid());
//                    System.out.println("APPRAISAL: " + appraisal);
                    if (appraisal != null) {
                        colllist = apd.getCollsPerAppraisalId(appraisal.getAppraisalid());
                        System.out.print("COLLIST:" + colllist);
                    } else {
                        actionstat = "noap";
                    }

                } catch (Exception e) {
                }

            }

        }

        return SUCCESS;
    }
}
